package com.gx.smart.building.ui.warning.viewmodel

import android.app.Application
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.casic.gx.grpc.common.ComQuery
import com.gx.smart.building.AlarmApiService
import com.gx.smart.building.AlarmDashboardApiService
import com.gx.smart.building.SysDictApiService
import com.gx.smart.building.WorkOrderApiService
import com.gx.smart.building.ui.common.viewmodel.CommonViewModel
import com.gx.smart.common.util.AppConfig
import com.gx.smart.lib.http.api.RetrofitManager
import com.gx.smart.lib.http.api.asynctask.CallBack
import com.gx.smart.lib.http.api.config.ApiConfig
import com.gx.smart.lib.http.api.response.MessageAlarm
import com.huishi.gx.ib.grpc.lib.ib.admin.maintenance.messagealarm.alarmdashboard.v1.AlarmDashboardDataDto
import com.huishi.gx.ib.grpc.lib.ib.admin.maintenance.messagealarm.alarmdashboard.v1.QueryAlarmResolveStateTotalResp
import com.huishi.gx.ib.grpc.lib.ib.admin.maintenance.messagealarm.v1.AlarmDealResp
import com.huishi.gx.ib.grpc.lib.ib.admin.maintenance.messagealarm.v1.MessageAlarmDto
import com.huishi.gx.ib.grpc.lib.ib.app.workorder.v1.InsertWorkOrderResp
import com.huishi.gx.ib.grpc.lib.ib.app.workorder.v1.dto.WorkOrderDto
import com.huishi.gx.ib.grpc.lib.ib.app.workorder.v1.dto.WorkOrderFileDto
import com.huishi.gx.ib.grpc.lib.sysdict.sysdictapi.SysDictQueryByParentResp
import com.huishi.gx.ib.grpc.lib.sysdict.sysdictapi.SysDictQueryItemDto
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

/**
 *@author: xiaosy
 *时间: 2022/4/7
 *@Description: java类作用描述
 *@Version: 1.0
 **/
class WarningViewModel(app: Application) : CommonViewModel(app) {
    private val title: MutableLiveData<List<AlarmDashboardDataDto>> = MutableLiveData()
    val listUntreated: MutableLiveData<List<MessageAlarm>> = MutableLiveData()
    val listProcess: MutableLiveData<List<MessageAlarm>> = MutableLiveData()
    private val detail: MutableLiveData<MessageAlarm> = MutableLiveData()
    private val dealSuccess: MutableLiveData<Boolean> = MutableLiveData()
    private val addSuccess: MutableLiveData<WorkOrderDto> = MutableLiveData()
    private val typeList: MutableLiveData<List<SysDictQueryItemDto>> = MutableLiveData()
    private val subTypeList: MutableLiveData<List<SysDictQueryItemDto>> = MutableLiveData()

    //今日告警等级总数
    fun queryAlarmLevelTotal(): LiveData<List<AlarmDashboardDataDto>> {
        val userId = SPUtils.getInstance().getString(AppConfig.USER_ID)
        AlarmDashboardApiService.getInstance()
            .queryAlarmResolveStateTotal(
                userId,
                object : CallBack<QueryAlarmResolveStateTotalResp>() {
                    override fun callBack(result: QueryAlarmResolveStateTotalResp?) {
                        if (result == null) {
                            ToastUtils.showLong("服务器超时")
                            return
                        }
                        val comResp = result.comResp
                        if (comResp.code == 100) {
                            title.value = result.contentList
                        } else {
                            handleServiceException(comResp)
                    }
                }
            })
        return title
    }

    //查询告警列表
    fun query(currentPage: Int, status: Int) {
        val comQuery: ComQuery = ComQuery
            .newBuilder()
            .setPage(currentPage)
            .setPageSize(10)
            .build()
        val userId = SPUtils.getInstance().getString(AppConfig.USER_ID)
        val call = RetrofitManager.getInstance().alarmApiService.queryAlarmListByUser()
        call.enqueue(object: Callback<com.gx.smart.lib.http.api.response.AlarmQueryResp> {
            override fun onResponse(
                call: Call<com.gx.smart.lib.http.api.response.AlarmQueryResp>,
                response: Response<com.gx.smart.lib.http.api.response.AlarmQueryResp>
            ) {
                val result = response.body()
                if (result == null) {
                    ToastUtils.showLong("服务器超时")
                    return
                }
                val comResp = result.comResp
                if (comResp.code == 100) {
                    listUntreated.value = result.content
                } else {
                    ToastUtils.showLong(ApiConfig.SERVER_ERROR_MESSAGE)
                }
            }

            override fun onFailure(
                call: Call<com.gx.smart.lib.http.api.response.AlarmQueryResp>,
                t: Throwable
            ) {
                ToastUtils.showLong(ApiConfig.SERVER_ERROR_MESSAGE)
            }

        })
    }

    //查询告警列表
    fun query2(currentPage: Int, status: Int) {
        val comQuery: ComQuery = ComQuery
            .newBuilder()
            .setPage(currentPage)
            .setPageSize(10)
            .build()
        val userId = SPUtils.getInstance().getString(AppConfig.USER_ID)
        val call = RetrofitManager.getInstance().alarmApiService.queryAlarmListByUser()
        call.enqueue(object: Callback<com.gx.smart.lib.http.api.response.AlarmQueryResp> {
            override fun onResponse(
                call: Call<com.gx.smart.lib.http.api.response.AlarmQueryResp>,
                response: Response<com.gx.smart.lib.http.api.response.AlarmQueryResp>
            ) {
                val result = response.body()
                if (result == null) {
                    ToastUtils.showLong("服务器超时")
                    return
                }
                val comResp = result.comResp
                if (comResp.code == 100) {
                    listProcess.value = result.content
                } else {
                    ToastUtils.showLong(ApiConfig.SERVER_ERROR_MESSAGE)
                }
            }

            override fun onFailure(
                call: Call<com.gx.smart.lib.http.api.response.AlarmQueryResp>,
                t: Throwable
            ) {
                ToastUtils.showLong(ApiConfig.SERVER_ERROR_MESSAGE)
            }

        })
    }

    //查看告警详情
    fun detail(id: String): LiveData<MessageAlarm> {

        val call = RetrofitManager.getInstance().alarmApiService.detail()
        call.enqueue(object: Callback<com.gx.smart.lib.http.api.response.AlarmDetailQueryResp> {
            override fun onResponse(
                call: Call<com.gx.smart.lib.http.api.response.AlarmDetailQueryResp>,
                response: Response<com.gx.smart.lib.http.api.response.AlarmDetailQueryResp>
            ) {
                val result = response.body()
                if (result == null) {
                    ToastUtils.showLong("服务器超时")
                    return
                }
                val comResp = result.comResp
                if (comResp.code == 100) {
                    detail.value = result.content
                } else {
                    ToastUtils.showLong(ApiConfig.SERVER_ERROR_MESSAGE)
                }
            }

            override fun onFailure(
                call: Call<com.gx.smart.lib.http.api.response.AlarmDetailQueryResp>,
                t: Throwable
            ) {
                ToastUtils.showLong(ApiConfig.SERVER_ERROR_MESSAGE)
            }

        })

        return detail
    }

    //处理告警
    fun deal(detail: MessageAlarmDto): LiveData<Boolean> {
        AlarmApiService.getInstance().deal(detail, object : CallBack<AlarmDealResp>() {
            override fun callBack(result: AlarmDealResp?) {
                if (result == null) {
                    ToastUtils.showLong("服务器超时")
                    dealSuccess.value = false
                    return
                }
                val comResp = result.comResp
                if (comResp.code == 100) {
                    dealSuccess.value = true
                } else {
                    handleServiceException(comResp)
                    dealSuccess.value = false
                }
            }
        })
        return dealSuccess
    }

    //通过父级的字典编码或者是父级的字典ID查询子集字典列表（只查一级）
    fun queryDict(
        parentTag: String
    ): LiveData<List<SysDictQueryItemDto>> {
        SysDictApiService.getInstance()
            .queryDictByParentId(
                parentTag,
                object : CallBack<SysDictQueryByParentResp>() {
                    override fun callBack(result: SysDictQueryByParentResp?) {
                        if (result == null) {
                            ToastUtils.showLong("服务器超时")
                            return
                        }
                        val comResp = result.comResp
                        if (comResp.code == 100) {
                            typeList.value = result.contentList
                        } else {
                            handleServiceException(comResp)
                        }
                    }
                })
        return typeList
    }

    //通过父级的字典编码或者是父级的字典ID查询子集字典列表（只查一级）
    fun queryDictByParentId(
        parentTag: String
    ): LiveData<List<SysDictQueryItemDto>> {
        SysDictApiService.getInstance()
            .queryDictByParentId(
                parentTag,
                object : CallBack<SysDictQueryByParentResp>() {
                    override fun callBack(result: SysDictQueryByParentResp?) {
                        if (result == null) {
                            ToastUtils.showLong("服务器超时")
                            return
                        }
                        val comResp = result.comResp
                        if (comResp.code == 100) {
                            subTypeList.value = result.contentList
                        } else {
                            handleServiceException(comResp)
                        }
                    }
                })
        return subTypeList
    }

    //新增工单
    fun insertWorkOrder(
        orderTypeId: String,
        orderTypeCode: String,
        orderTypeName: String,
        orderSubTypeId: String,
        orderSubTypeCode: String,
        orderSubTypeName: String,
        areaName: String,
        description: String,
        releaseId: String,
        releaseUserName: String,
        releasePhone: String,
        files: List<WorkOrderFileDto>,
        assetId: String,
        assetName: String
    ): LiveData<WorkOrderDto> {
        WorkOrderApiService.getInstance()
            .insertWorkOrder(
                orderTypeId,
                orderTypeCode,
                orderTypeName,
                orderSubTypeId,
                orderSubTypeCode,
                orderSubTypeName,
                areaName,
                description,
                releaseId,
                releaseUserName,
                releasePhone,
                files,
                assetId,
                assetName,
                object : CallBack<InsertWorkOrderResp?>() {
                    override fun callBack(result: InsertWorkOrderResp?) {
                        if (result == null) {
                            ToastUtils.showLong("服务器超时")
                            return
                        }
                        val comResp = result.comResp
                        if (comResp.code == 100) {
                            addSuccess.value = result.workOrderDto
                        } else {
                            handleServiceException(comResp)
                        }
                    }
                })
        return addSuccess
    }


}